from Acquisition import aq_inner

from Products.Five.browser import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Products.CMFCore.utils import getToolByName
from plone.memoize.instance import memoize
from uriach.content.interfaces import IUriachFile

class UriachFileView(BrowserView):
    """Default view of a uriachfile news item
    """
    
    __call__ = ViewPageTemplateFile('uriachfile.pt')
    
    def have_uriachfile_news(self):
        return len(self.uriachfile_contents()) > 0
        
    @memoize
    def uriachfile_news(self):
        context = aq_inner(self.context)
        
        return [dict(url=uriachfile.absolute_url(),
                     title=uriachfile.title,
                     desc=uriachfile.description,
                     summary=uriachfile.summary,)
                for uriachfile in context.uriachfile_news]
    
    def uriachfile_contents(self):
        portal_catalog = getToolByName(self, 'portal_catalog')          
        list = portal_catalog.searchResults(portal_type = 'UriachFile',
                                             path = '/'.join(self.context.getPhysicalPath()),
                                             review_state=['internally_published','external','published'],
                                             sort_on='getObjPositionInParent')
        return list